rm(list = ls())

# Set Working Directory to Replication Folder
taskdir 		<- "~/Dropbox/final_hai_perlman_replication/"



setwd(taskdir)
library(tidyverse)
library(lfe)
require(data.table)
library(stringi)
library(ebal)


# Weight by Party (for Pooled Regression), Gender, Race, 
#	College Attendance
# PEW research center party identification 2019
# https://docs.google.com/spreadsheets/d/1v58Nt6SW832ZUq7xLI6N8qdDs3qN4qWoDU-2EyEOGZg/edit#gid=2104546316
pc_republican <- 0.29
pc_democrat <- 0.33
pc_independent <- 0.34
# American Community Survey, 5-year Estimates, 2020
pc_white  	<- 0.62
pc_female 	<- 0.51
# Current Population Survey (ACS 1-Year Estimates, 2019)
pc_college	<- 0.43

# Subgroup percentages (also from PEW)
pc_college_r <- 0.23*pc_college
pc_college_d <- 0.36*pc_college
pc_college_i <- 0.37*pc_college
pc_nocollege_r <- 0.32*(1-pc_college)
pc_nocollege_d <- 0.31*(1-pc_college)
pc_nocollege_i <- 0.33*(1-pc_college)

## load survey 1
survey1 <- read_csv(paste0(taskdir, 'input/survey1.csv'))%>%
			mutate(independent = ifelse(republican==0 & democrat==0,1,0),
				   party = case_when(
				   		republican ==1 ~ "Republican",
				   		democrat ==1 ~ "Democrat",
				   		independent==1 ~ "Independent",
				   		TRUE ~ as.character(NA)
				   	))%>%
			group_by(college, party)%>%
			mutate(sample_proportion = n()/3103)%>%
			# Create weights  = population proportion / sample proportion
			mutate(weight = case_when(
				   			republican ==1 & college==0 ~ pc_nocollege_r/sample_proportion,
				   			democrat   ==1 & college==0 ~ pc_nocollege_d/sample_proportion,
				   			independent==1 & college==0 ~ pc_nocollege_i/sample_proportion, 
				   			republican ==1 & college==1 ~ pc_college_r/sample_proportion,
				   			democrat   ==1 & college==1 ~ pc_college_d/sample_proportion,
				   			independent==1 & college==1 ~ pc_college_i/sample_proportion, 				   			
				   			TRUE ~ as.numeric(NA)
				   	))




#---------- Run Regressions
# Define all DVs
dvs <- c('tax_support' ,'politician_prevent' ,'politician_understand' ,
		'politician_advocate' ,'politician_sympathy')

# Regression specification
rhs <- ' ~ treat+female+ white+ college|income|0 | 0'
# Run Regressions
outlist <- list()
for (dv in dvs) {
	fmla <- paste0(dv,rhs)%>%as.formula
	# Politician is a Republican
	mod1 <- felm(fmla, weights = survey1%>%pull(weight), 
						data = survey1)
	mod2 <- felm(fmla, weights = survey1%>%filter(republican == 1)%>%pull(weight), 
						data = survey1%>%filter(republican == 1))
	mod3 <- felm(fmla, weights = survey1%>%filter(democrat == 1)%>%pull(weight), 
						data = survey1%>%filter(democrat == 1))
	mod4 <- felm(fmla, weights = survey1%>%filter(republican == 0 & democrat==0)%>%pull(weight), 
						data = survey1%>%filter(republican == 0 & democrat==0))

	# save models in list 
	templsit <- list(mod1, mod2, mod3, mod4)
	outlist[[dv]]  <- templsit

}
save(outlist, 
	file = paste0(taskdir, 'input/processing/regressions_survey1/ols_survey1_weights.RData'))










